<?php

namespace App\Console;

use App\Jobs\Bookjob;
use App\Jobs\BookSortJob;
use App\Models\User;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\DB;
use function App\Helpers\{getLastEndWeekDate,getLastStartWeekDate,successMsg};
use Illuminate\Support\Facades\Log;

class Kernel extends ConsoleKernel
{
    /**
     * The Artisan commands provided by your application.
     *
     * @var array
     */
    protected $commands = [
        //
    ];

    /**
     * Define the application's command schedule.
     *
     * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
     * @return void
     */
    protected function schedule(Schedule $schedule)
    {
        // $schedule->command('inspire')->hourly();
        //【书籍上传】每十五分钟 执行一次
        $schedule->call(function () {
            $book_task = $this->getBookTask();
            if(!empty($book_task)){
                Bookjob::dispatch($book_task['path'],$book_task['year'],$book_task['book_id'],$book_task['book_uni_code']);
            }
        })->everyFifteenMinutes();

        //【章节排序】每十分钟 执行一次
        $schedule->call(function () {
            $book_sort_task = $this->getBookSortTask();
            if(!empty($book_sort_task)){
               BookSortJob::dispatch($book_sort_task['chapter_table'],$book_sort_task['book_id']);
            }
        })->everyTenMinutes();
    }

    /**
     * Register the commands for the application.
     *
     * @return void
     */
    protected function commands()
    {
        $this->load(__DIR__.'/Commands');

        require base_path('routes/console.php');
    }
    //查询出来 是否有上传的书籍任务
    public function getBookTask()
    {
        $book_upload = DB::table("book_upload")
            ->where('status',0)
            ->orderBy("id","asc")->first();
        if($book_upload){
            $book = DB::table("book")->where("id",$book_upload->book_id)->first();
            return [
                'book_id' => $book->id,
                'year' => $book->year,
                'book_uni_code' => $book->uni_code,
                'path' => $book_upload->path
            ];
        }
        return null;
    }
    // 查询出来 是否有需要做排序的数据
    public function getBookSortTask()
    {
        $book_sort = DB::table("book_sort")->first();
        if(empty($book_sort)) return null;
        $book = DB::table("book")->where('id',$book_sort->book_id)->first();
        if(empty($book)) return null;
        return [
            'chapter_table' => Config::get("site.chapter_table").$book->year,
            'book_id' => $book->id,
        ];
    }
    //处理昨天的数据
    public function executeYesterdayData($type)
    {
        $chapter_table = Config::get("site.chapter_table").date("Y");
        $content_table = Config::get("site.content_table").date("Y");
        $user_where = [];
        $book_where = [];
        $chapter_where = [];
        switch ($type)
        {
            case "day":
                $yesterday = date("Y-m-d",strtotime("-1 day"));
                $start_time = $yesterday.' 00:00:00';
                $end_time = $yesterday.' 59:59:59';
                $user_where[]= ['created_at','>=',$start_time];
                $user_where[]= ['created_at','<=',$end_time];
                $book_where[] = ['created_at','>=',$start_time];
                $book_where[] = ['created_at','<=',$end_time];
                $chapter_where[] = ['created_at','>=',$start_time];
                $chapter_where[] = ['created_at','<=',$end_time];
                break;
            case "week":

                break;
        }
        var_dump($user_where);
        var_dump($book_where);
        var_dump($chapter_where);
//        $user_total = User::query()->whereBetween('created_at',[$start_time,$end_time])->count("id") ?? 0;
//        $chapter_total = DB::table($chapter_table)->whereBetween('created_at',[$start_time,$end_time])->count("id") ?? 0;
//        $chapter_total = DB::table($content_table)->whereBetween('created_at',[$start_time,$end_time])->count("id") ?? 0;



    }
}
